package com.ulook.lookplanet.ui.notifications.friend;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.ulook.lookplanet.base.BaseViewModel2;
import com.ulook.lookplanet.common.AppServer;
import com.ulook.lookplanet.data.model.TUser;
import com.ulook.lookplanet.util.json.JacksonUtil;
import com.ulook.lookplanet.util.net.AsynRequestUtil;
import com.ulook.lookplanet.util.net.OkHttpException;
import com.ulook.lookplanet.util.net.RequestParams;
import com.ulook.lookplanet.util.net.ResponseCallback;

import java.util.List;

/**
 * 描述：
 *
 * @author L1998
 * @data 2021/5/10
 */
public class FriendViewModel extends BaseViewModel2 {

    private int currUserId;
    private MutableLiveData<List<TUser>> friends = new MutableLiveData<>();

    public int getCurrUserId() {
        return currUserId;
    }

    public void setCurrUserId(int currUserId) {
        this.currUserId = currUserId;
    }

    public LiveData<List<TUser>> getFriends(){
        return  friends;
    }

    /**
     * 获取关注列表
     */
    public void getFriendList(){
        ResponseCallback callback = new ResponseCallback() {
            @Override
            public void onSuccess(Object res) {
                try {
                    List<TUser> users = JacksonUtil.json2list((String)res, TUser.class);
                    friends.setValue(users);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(OkHttpException failuer) {
                setToastText("查询失败");
            }
        };
        RequestParams params = new RequestParams();
        params.put("id", currUserId);
        AsynRequestUtil.post(AppServer.ROOT_PATH + "/home/getFriends", params, null, callback);
    }
}
